package cn.zcscloud.mall.controller;

import cn.zcscloud.mall.common.PageRequest;
import cn.zcscloud.mall.common.R;
import cn.zcscloud.mall.mbg.model.PmsBrand;
import cn.zcscloud.mall.service.BrandService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * ClassName: BrandContorller
 * Description:
 * date: 2022/4/1 11:30
 *
 * @author zjf95
 * @since JDK 1.8
 */
@RestController
@RequestMapping("/brand")
@Slf4j
public class BrandController {

    @Autowired
    private BrandService brandService;

    @GetMapping("/list")
    public R list(String keyword, int pageNum, int pageSize) {

        log.info("前端品牌的数据分页为：{}", pageNum + pageSize);

        PageRequest page = brandService.page(keyword, pageNum, pageSize);

        return R.success("操作成功", page);
    }

    @GetMapping("/{id}")
    public R one(@PathVariable Long id) {

        log.info("要更新的品牌id为：{}", id);
        PmsBrand pmsBrand = brandService.findById(id);
        return R.success("操作成功", pmsBrand);
    }

    @PostMapping("/update/{id}")
    public R update(@PathVariable Long id, @RequestBody PmsBrand pmsBrand) {

        log.info("要更新的品牌id为：{}", id);
        log.info("要修改的数据为{}", pmsBrand);

        brandService.update(pmsBrand);

        return R.success("操作成功", null);
    }

    @PostMapping("/update/factoryStatus")
    public R updateStatus(Integer[] ids, Integer factoryStatus) {

        log.info("批量的id为：{}", ids);
        log.info("修改的状态为：{}", factoryStatus);
        if (ids.length == 0 || factoryStatus == null) {
            return R.error("数据传输失败");
        }
        brandService.updateByIds(ids, factoryStatus);

        return R.success("操作成功", null);
    }

    @PostMapping("/update/showStatus")
    public R updateShow(Integer[] ids, Integer showStatus) {
        if (ids.length == 0 || showStatus == null) {
            return R.error("数据传输失败");
        }
        brandService.updateShow(ids, showStatus);

        return R.success("操作成功", null);
    }

    /**
    * Description: 未完成
    * @author: zjf95
    * @date: 2022/4/3 10:07
    * @param:
    * @return:
    */
    @GetMapping("/delete/{id}")
    public R delete(@PathVariable Integer id) {

        brandService.deleteById(id);

        return R.success("操作成功", null);
    }

    /**
     * Description: 批量删除，但是前端没有展示，所以不写
     *
     * @author: zjf95
     * @date: 2022/4/1 19:50
     * @param:
     * @return:
     */
    @PostMapping("/delete/batch")
    public R deleteSome(@RequestBody Integer[] ids) {

        log.info("davidquvafjda:{}", ids);
        return null;
    }

    @PostMapping("/create")
    public R create(@RequestBody PmsBrand pmsBrand){

        if (pmsBrand == null) {
            return R.error("数据有误");
        }

        log.info("新建的数据：{}",pmsBrand);

        brandService.add(pmsBrand);

        return R.success("操作成功",null);

    }

}
